#include "loginwindow.h"
#include "labelbutton.h"

#include <QVBoxLayout>
#include <QtDebug>

LoginWindow::LoginWindow( QWidget * parent ) : WhaleWindow( parent ) {
    // display loading label and progress bar
    this->mLoadingLabel = new QLabel( "Loading twitter..." );
    this->mLoadBar = new QProgressBar();

    // the WebKit browser for logging in (hidden when loading)
    this->mBrowser = new LoginBrowser();
    this->mBrowser->loadLogin();
    this->mBrowser->hide();

    // one layout with all items
    QVBoxLayout * layout = new QVBoxLayout();
    layout->addWidget( this->mLoadBar );
    layout->addWidget( this->mLoadingLabel );
    layout->addWidget( this->mBrowser );

    // a central window widget
    QWidget * central = new QWidget();
    central->setLayout( layout );

    this->setCentralWidget( central );

    // display browser when user authenticates and loading messages when browser loads
    // connect progressbar with browser loading progress
    connect( this->mBrowser, SIGNAL( loadProgress( int ) ), this->mLoadBar, SLOT( setValue( int ) ) );
    // connect( this->mBrowser, SIGNAL( loginLoadFinished() ), this->mLoadingLabel, SLOT( hide() ) );
    // connect( this->mBrowser, SIGNAL( loginLoadFinished() ), this->mLoadBar, SLOT( hide() ) );
    // connect( this->mBrowser, SIGNAL( loginLoadFinished() ), this->mBrowser, SLOT( show() ) );
    connect( this->mBrowser, SIGNAL( loginLoadFinished() ), this, SLOT( showMaximized() ) );
    connect( this->mBrowser, SIGNAL( authenticationStarted() ), this->mBrowser, SLOT( hide() ) );

    connect( this->mBrowser, SIGNAL( loadFinished( bool ) ), this->mBrowser, SLOT( show() ) );
    connect( this->mBrowser, SIGNAL( loadFinished( bool ) ), this->mLoadBar, SLOT( hide() ) );
    connect( this->mBrowser, SIGNAL( loadFinished( bool ) ), this->mLoadingLabel, SLOT( hide() ) );
}
